<?php
/**
* @author zhaorongwei 
* @date 2012-06-18
* @description 用户相关
*/
class UserController extends Controller
{
	private $errors;
	public function __construct()
	{
		$this->model=loadModel('user');
	}

	//添加用户
	public function userAdd()
	{
		$vars = array('pageTitle' => '添加用户');
		
		if(isPost())
		{
			if(false === $this->checkUserAdd())
			{
				$vars['errors'] = $this->errors;
			}
			else
			{
				$data=array();
				$data['fusername']=post('username');
				$data['fpassword']=md5(post('password'));
				$data['flasttime']=$data['fregtime']=time();
				$data['flastip']=$data['fregip']=ip();
				$data['fgroupid']=intval(post('groupid'));
				$data['fmobile']=post('mobile');
				$data['femail']=post('email');
				if($this->model->userAdd($data))
				{
					redirect('user/userlist');
				}
			}
			$vars['username'] = post('username');
		}
		$groups=$this->model->getUserGroups();
		$vars['usergroup']=htmlSelect2('groupid',$groups,'fid','fsubject',post('groupid'));			
		template('user/useradd', $vars);
	}

	/**
	 * 验证新增用户
	 * @return bool
	 */
	private function checkUserAdd()
	{
		if(post('username') == '')
		{
			$this->errors = '请输入注册用户名';
		}
		elseif(post('password') == '')
		{
			$this->errors = '请输入您要设置的密码';
		}
		elseif(post('password') != post('repassword'))
		{
			$this->errors = '两次密码输入不一样，请确认';
		}
		elseif($this->model->userIsExist())
		{
			$this->errors = '用户名已存在';
		}
		else
		{
			return true;
		}
		return false;
	}

	//修改用户
	public function userEdit()
	{
		$vars = array('pageTitle' => '修改用户');
		$id=intval(request('id'));	
		$uinfo=$this->model->getUserInfo($id);
		if(!$uinfo)
		{
			$vars['errors']="用户不存在";
		}
		$vars['username']=$uinfo['fusername'];
		$vars['id']=$uinfo['fid'];
		$vars['email']=$uinfo['femail'];
		$vars['mobile']=$uinfo['fmobile'];

		if(isPost())
		{
			$id=post('id');
			$username=post('username');
			$groupid=post('groupid');
			$mobile=trim(post('mobile'));
			$email=trim(post('email'));
			if(!$id)
			{
				$vars['errors'] = "用户编号不能为空"; 
			}
			else if(!$username)
			{
				$vars['errors']="用户名不能为空";
			}
			else if(!$groupid)
			{
				$vars['errors']='用户组不能为空';
			}
			else
			{
				if($this->model->userEdit($id,array('fusername'=>$username,'fgroupid'=>$groupid,'fmobile'=>$mobile,'femail'=>$email)))
				{
					redirect('user/userlist');
				}
			}
			$vars['username'] = post('username');
		}
		$groups=$this->model->getUserGroups();
		$vars['usergroup']=htmlSelect2('groupid',$groups,'fid','fsubject',$uinfo['fgroupid']);			
		template('user/useredit', $vars);
	}

	//删除用户
	public function userDel()
	{
		$id=intval(get('id'));
		if($id && in_array('userdel',$GLOBALS['QYUserAuth']['func']))
		{
			$this->model->userDel($id);
		}	
		redirect('user/userlist');
	}
	
	//修改密码,绑定邮箱手机号等
	public function changePassword()
	{
		$vars=array('pageTitle'=>'用户设置');
		if(isPost())
		{
			$this->errors='';
			if(post('username')=='')
			{
				$this->errors='用户名不能为空';
			}
			else if(post('password')=='')
			{
				$this->errors='请输入登录密码';
			}
			else if(post('mobile')=='' && post('email')=='')
			{
				$this->errors='请输入手机号和邮箱';
			}
			else if(!$this->model->checkLogin(post('username'),post('password')))
			{
				$this->errors='您输入的登录密码不正确';
			}

			if($this->errors)
			{
				$vars['errors']=$this->errors;
			}
			else
			{
				if($this->model->updateUser(array('fpassword'=>md5(post('repassword')),'fmobile'=>post('mobile'),'femail'=>post('email')),array('fusername'=>post('username'))))
				{
					$vars['errors']='用户信息修改成功!';
				}
			}

		}
		$uid=intval(request('id'));
		$uname='';
		if($uid)
		{
			$uinfo=$this->model->getUserInfo($uid);
			if($uinfo)
			{
				$uname=$uinfo['fusername'];
			}
		}
		$vars['username']=($uid && $uname)?$uname:loginInfo('uname');
		if(!$uinfo)
		{
			$uinfo=$this->model->getUserInfo(loginInfo('uid'));
		}
		$vars['mobile']=$uinfo['fmobile'];
		$vars['email']=$uinfo['femail'];
		template('user/changepassword', $vars);
	}	

	//用户列表
	public function userList()
	{
		$vars = array(
			'pageTitle' => '用户列表',
			);
		$vars['userList'] = $this->model->getUserList();
		$auth_funcs=$GLOBALS['QYUserAuth']['func'];
		$vars['candel']=in_array('userdel',$auth_funcs);
		$vars['canedit']=in_array('useredit',$auth_funcs);
		$vars['candel']=$vars['canedit']=true;
		
		template('user/userlist', $vars);
	}
	
		
	//登录
	public function login()
	{
		$vars = array(
			'pagetitle' => '用户登录',
			'username' => ''
			);
		if(isPost())
		{
			$rt = $this->model->checkLogin(post('username'),post('password'));
			if($rt)
			{
				$userGroup=$this->model->getUserGroupTitle($rt['fgroupid']);
				$time = time()+86400;
				setcookie('admin_uid', $rt['fid'], $time);
				setcookie('admin_uname', $rt['fusername'], $time);
				setcookie('admin_group',$userGroup,$time);
				$this->model->userEdit($rt['fid'],array('flasttime' => time(), 'flastip' => ip()));
				redirect('/');
			}
			else
			{
				$vars['error'] = true;
				$vars['username'] = post('username');
			}
		}
		template('user/login', $vars);
	}	

	//退出登录
	public function logout()
	{
		setcookie('admin_uid', '', 0);
		setcookie('admin_uname', '', 0);
		setcookie('admin_group', '', 0);
		redirect('user/login');
	}

	//新增用户组
	public function userGroupAdd()
	{
		$vars = array(
			'pageTitle' => '添加用户组'
			);
		if(isPost())
		{
			$subject=post('subject');
			if(!$subject)
			{
				$vars['errors']='请输入用户组';
			}
			else if($this->model->getUserGroupByName($subject)) 
			{
				$vars['errors']='用户组已存在,新增失败!';	
			}
			else
			{
				$this->model->userGroupAdd($subject);
				redirect('user/usergrouplist');
			}
			exit;
		}
		template('user/usergroupedit', $vars);
	}

	//修改用户组
	public function userGroupEdit()
	{
		$vars = array(
			'pageTitle' => '修改用户组'
		);
		$offset=intval(request('offset'));
		$id=intval(request('id'));
		if(!$id)
		{
			redirect('user/usergrouplist','offset='.$offset);
		}

		if(isPost())
		{
			$subject=post('subject');	
			if($subject)
			{
				$tmp=$this->model->getUserGroupByName($subject);
				if($tmp && $tmp['fid']!=$id)
				{
					$vars['errors']='用户组已存在,修改失败!';
				}
				else
				{
					$this->model->userGroupUpdate($id,array('fsubject'=>$subject));
					redirect('user/usergrouplist','offset='.$offset);
				}
			}
			else
			{
				$vars['errors']='请输入组名称';
			}
		}
		if($id)	
		{
			$rec=$this->model->getUserGroupById($id);
		}
		if(!$rec)
		{
			redirect('user/usergrouplist','offset='.$offset);
		}
		$vars['subject']=$rec['fsubject'];
		$vars['id']=$rec['fid'];
		template('user/usergroupedit', $vars);
	}
   	//用户组列表 
	public function userGroupList()
	{
		$vars = array(
			'pageTitle' => '用户组列表'
			);
		$offset=intval(request('offset'));
		if($offset<1)
			$offset=0;
		$limit=intval(request('limit'));
		if(!$limit)
			$limit=20;
		
		$vars['total']=$this->model->userGroupTotal();
		$vars['groupList']=$this->model->getUserGroupList($offset,$limit);
		if(in_array('usergroupedit',$GLOBALS['QYUserAuth']['func']))
		{
			$vars['actions'][]=array('修改',getUrl('user/usergroupedit','id={id}&offset='.$offset));
		}
		if(in_array('setauth',$GLOBALS['QYUserAuth']['func']))
		{
			$vars['actions'][]=array('设置权限',getUrl('auth/setauth','id={id}&offset='.$offset));
		}
		if(in_array('usergroupdel',$GLOBALS['QYUserAuth']['func']))
		{
			$vars['actions'][]=array('删除',getUrl('user/usergroupdel','id={id}&offset='.$offset),'onclick="return QY.util.del_confirm();"');
		}
		if($total>$limit)
		{
			$vars['pageLink']=getPageLink($total,"limit=$limit","offset=$offset","r=".request('r'));
		}

		template('user/usergrouplist', $vars);
	}

	public function userGroupDel()
	{
		$id=intval(request('id'));
		if($id)
		{
			$this->model->userGroupDel($id);
		}
		redirect('user/usergrouplist','offset='.get('offset'));
	}
}
